﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VacationInfo : System.Web.UI.Page
    {
        protected ReqVacationMain reqVacationMain;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            string formNo = Request.QueryString["formNo"];
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<ReqVacationMain>(r => r.ReqVacationSubs);
            ctx.LoadOptions = dlo;
            reqVacationMain = ctx.ReqVacationMains.Single(r => r.FormNo == formNo);

            rptList.DataSource = reqVacationMain.ReqVacationSubs;
            rptList.DataBind();
            Employee employee = ctx.Employees.Single(r => r.Id == reqVacationMain.EmpId);
            empName = employee.Name;

            int empId = employee.Id;
            string sql = string.Format(@"select top 3 b.Id,b.FormNo,b.PreStartTime,b.PreEndTime,a.ReplacedDay Remarks  from ReqVacationMains a,ReqVacationSubs b where a.FormNo=b.FormNo and a.EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE()) and a.VacationType='调休' and (a.StatusId=2 or a.StatusId=5) order by PreStartTime desc;", empId);
            IEnumerable<ReqVacationSub> reqVacationSubs = ctx.ExecuteQuery<ReqVacationSub>(sql);
            rptList0.DataSource = reqVacationSubs;
            rptList0.DataBind();

            sql = string.Format(@"select top 3 * from ReqOvertime
  where EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE())
  order by FormNo desc;", empId);
            IEnumerable<ReqOvertime> reqOvertimes = ctx.ExecuteQuery<ReqOvertime>(sql);
            rptList2.DataSource = reqOvertimes;
            rptList2.DataBind();

            sql = string.Format(@"select top 3 * from duty
  where EmpId={0} and dutydate>=DATEADD(M,-6,GETDATE())
  order by id desc;", empId);
            IEnumerable<Duty> duty = ctx.ExecuteQuery<Duty>(sql);
            rptList3.DataSource = duty;
            rptList3.DataBind();

            Common common = new Common();
            int nianJiaTotal = common.GetNianJia(empId.ToString());
            lblNianJiaTotal.InnerText = nianJiaTotal.ToString();

            sql = string.Format(@"SELECT ISNULL(SUM(DaysCnt),0)
  FROM ReqVacationMains a,ReqVacationSubs b
  where a.FormNo=b.FormNo and a.EmpId={0} and VacationType='年假' and b.PreStartTime>='{1}' and StatusId=2;", empId.ToString(), DateTime.Today.ToString("yyyy-01-01"));
            lblNianJia.InnerText = ctx.ExecuteQuery<decimal>(sql).First().ToString();
        }
    }
}